/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ThemTuyenBayFrm.java
 *
 * Created on May 28, 2011, 1:07:10 AM
 */
package gui;

import dao.SanBayDAO;
import dao.TuyenBayDAO;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import pojo.SanBayPOJO;
import pojo.TuyenBayPOJO;

/**
 *
 * @author b2
 */
public class ThemTuyenBayFrm extends javax.swing.JFrame {

    private ArrayList<SanBayPOJO> ds;

    /** Creates new form ThemTuyenBayFrm */
    public ThemTuyenBayFrm() {
        initComponents();
    }

    public void initThemTuyenBay() {
        ds = SanBayDAO.LayDanhSachSanBay();
        String[] dsSanBay = new String[ds.size()];
        for (int i = 0; i < ds.size(); i++) {
            SanBayPOJO sb = ds.get(i);
            dsSanBay[i] = sb.getTenSanBay();
        }
        DefaultComboBoxModel sbdi = new DefaultComboBoxModel(dsSanBay);
        DefaultComboBoxModel sbde = new DefaultComboBoxModel(dsSanBay);
        this.jcbSanBayDi.setModel(sbdi);
        this.jcbSanBayDen.setModel(sbde);
        this.layMaSanBay();
    }

    public void layMaSanBay() {
        int intSBDi = this.jcbSanBayDi.getSelectedIndex();
        int intSBDe = this.jcbSanBayDen.getSelectedIndex();
        String maSanBayDi = ds.get(intSBDi).getMaSanBay();
        String maSanBayDen = ds.get(intSBDe).getMaSanBay();
        String maTuyenBay = maSanBayDi + "-" + maSanBayDen;
        this.txtMaTuyenBay.setText(maTuyenBay);
    }

    public void themTuyenBay() {
        TuyenBayPOJO tb = new TuyenBayPOJO();
        String maTuyenBay = this.txtMaTuyenBay.getText();
        int intSBDi = this.jcbSanBayDi.getSelectedIndex();
        int intSBDe = this.jcbSanBayDen.getSelectedIndex();
        String maSanBayDi = ds.get(intSBDi).getMaSanBay();
        String maSanBayDen = ds.get(intSBDe).getMaSanBay();
        tb.setMaTuyenBay(maTuyenBay);
        tb.setSanBayDi(maSanBayDi);
        tb.setSanBayDen(maSanBayDen);
        boolean kq = TuyenBayDAO.themTuyenBay(tb);
        if (kq == true) {
            JOptionPane.showMessageDialog(this, "Thêm thành công");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtMaTuyenBay = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jcbSanBayDi = new javax.swing.JComboBox();
        jcbSanBayDen = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();
        btnThemSanBay = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Thêm tuyến bay");
        setBounds(new java.awt.Rectangle(200, 200, 0, 0));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setText("Thêm tuyến bay");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thêm tuyến bay"));

        jLabel2.setText("Mã tuyến bay");

        txtMaTuyenBay.setEditable(false);

        jLabel3.setText("Sân bay đi");

        jLabel4.setText("Sân bay đến");

        jcbSanBayDi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbSanBayDiActionPerformed(evt);
            }
        });

        jcbSanBayDen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbSanBayDenActionPerformed(evt);
            }
        });

        jButton1.setText("Thêm mới");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        btnThemSanBay.setText("Thêm sân bay");
        btnThemSanBay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemSanBayActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addGap(51, 51, 51)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jcbSanBayDen, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jcbSanBayDi, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtMaTuyenBay, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnThemSanBay, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnThoat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(25, 25, 25))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtMaTuyenBay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnThemSanBay))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jcbSanBayDi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton1)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jcbSanBayDen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnThoat))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(147, 147, 147))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        this.initThemTuyenBay();
    }//GEN-LAST:event_formWindowOpened

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnThoatActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.themTuyenBay();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jcbSanBayDiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbSanBayDiActionPerformed
        // TODO add your handling code here:
        this.layMaSanBay();
    }//GEN-LAST:event_jcbSanBayDiActionPerformed

    private void jcbSanBayDenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbSanBayDenActionPerformed
        // TODO add your handling code here:
        this.layMaSanBay();
    }//GEN-LAST:event_jcbSanBayDenActionPerformed

    private void btnThemSanBayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemSanBayActionPerformed
        // TODO add your handling code here:
        ThemSanBayFrm frm = new ThemSanBayFrm();
        frm.setVisible(true);
    }//GEN-LAST:event_btnThemSanBayActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ThemTuyenBayFrm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnThemSanBay;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JComboBox jcbSanBayDen;
    private javax.swing.JComboBox jcbSanBayDi;
    private javax.swing.JTextField txtMaTuyenBay;
    // End of variables declaration//GEN-END:variables
}
